/*
 * Created on 11.06.2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package de.localsport.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * @author Stephan
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Team implements Comparable {

	private int m_GoalDiff;
	private int m_Points;
	private int m_Id;
	private List m_Encounters;
	
	private class Encounter {		
		private int pm_Opponent;
		private int pm_Result;
		
		Encounter (int opponent, int result){
			pm_Opponent = opponent;
			pm_Result = result;
		}
		
		public int getOpponent(){
			return pm_Opponent;
		}
		
		public int getResult(){
			return pm_Result;
		}
	}

	public Team (int id) {
		m_Id = id;
		m_GoalDiff = 0;
		m_Points = 0;
		m_Encounters = Collections.synchronizedList(new ArrayList());
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */	
	public int compareTo(Object o){
		Team other = (Team) o;		
		
		if ( 
			 (this.getPoints()<other.getPoints()) || 
			 ((this.getPoints()==other.getPoints()) && (this.getEncounterResult(other.getId())<other.getEncounterResult(this.getId()))) || 	
		     ((this.getPoints()==other.getPoints()) && (this.getEncounterResult(other.getId())==other.getEncounterResult(this.getId()) && (this.getGoalDiff()<other.getGoalDiff())))
			) {
				return (int) -1;
		}
  		
		if ( 
			 (this.getPoints()==other.getPoints()) && (this.getGoalDiff()==other.getGoalDiff()) && (this.getEncounterResult(other.getId())==other.getEncounterResult(this.getId()))
		   ) {  		   				
				return (int) 0;
		}
		
		return (int) 1;  	
	}  	

	public void setGoalDiff (int i) {
		m_GoalDiff = i;
		return;
	}
	
	public void addGoals (int i) {
		m_GoalDiff += i;
		return;
	}
	
	public int getGoalDiff() {  		
		return m_GoalDiff;
	}
	
	public void setPoints (int i) {
		m_Points = i;
		return;
	}
	
	public void addPoints (int i) {
		m_Points += i;
		return;
	}
	
	public int getPoints() {  		
		return m_Points;
	}
	
	public int getId() {  		
		return m_Id;
	}
	
	public void addEncounter (int teamId, int points) {
		m_Encounters.add(new Encounter(teamId, points));
	}
	
	public int getEncounterResult (int teamId){
		
		for (Iterator it = m_Encounters.iterator(); it.hasNext();){
			Encounter encounter = (Encounter)it.next();
			if (encounter.getOpponent() == teamId){
				return encounter.getResult();
			}
		}
		
		return 0;
	}
}
